from goods.models import GoodsChannel


def get_category():
    categories = {}

    channels = GoodsChannel.objects.order_by("group_id", "sequence")

    for channel in channels:
        group_id = channel.group_id

        if group_id not in categories:
            categories[group_id] = {"channels": [], "sub_cats": []}
        catetory = channel.category
        catetory_dict = {
            "id": catetory.id,
            "name": catetory.name,
            "url": channel.url
        }
        categories[group_id]["channels"].append(catetory_dict)

        for cat2 in catetory.subs.all():
            categories[group_id]["sub_cats"].append(cat2)

    return categories